/**
 *  获取用户信息
 * */

import { useRequest } from 'umi';

import { getUserInfo } from '@/services/BaseLayout';

import { getPosIDs, getTenantID, getUserID } from '@/utils';

export const useWelcomeUserInfo = (homeDispatch: any) => {
  const userRequest = useRequest(
    () => {
      return getUserInfo({
        userId: getUserID(),
      });
    },
    {
      manual: true,
      onSuccess: (res) => {
        if (res && res?.obj) {
          const target = res?.obj?.muserLayoutList?.find((i: any) => {
            return (
              i?.layoutType === `${getTenantID()}10011001` &&
              i?.userPositionID == getPosIDs()
            );
          });

          homeDispatch({
            type: 'baseLogin/saveWelcomeLayout',
            payload: {
              welcomeLayout: target?.layoutInfo,
            },
          });

          homeDispatch({
            type: 'baseLogin/updateUserInfo',
            payload: res?.obj,
          });

          if (target) {
            // commonDispatch({
            //   type: 'update-layoutInfo',
            //   payload: target?.layoutInfo,
            // });

            homeDispatch({
              type: 'baseLogin/changeWelocmeLayout',
              payload: {
                openWelcomeLayout: {
                  isDraggable: false,
                  isResizable: false,
                  monitorLayout: false,
                },
              },
            });
            // homeDispatch({
            //   type: 'baseLogin/updateUserInfo',
            //   payload: res?.obj,
            // });
          }
        }
      },
    },
  );

  return {
    userObjInfo: userRequest?.data?.obj,
    loading: userRequest.loading,
    reloadUserInfo: userRequest.run,
  };

  // return {
  //   userInfo: userRequest?.data?.obj,
  //   loading: userRequest.loading,
  //   reloadUserInfo: userRequest.run,
  // };
};
